package com.unionfetch.content_home.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.unionfetch.content_home.R
import com.unionftech.common.utils.Utils

class CustomTipDialog(context: Context) : Dialog(context) {
    private val mContext = context
    private lateinit var mContentView: View
    private val mllDialogButtonContainer by lazy { mContentView.findViewById<LinearLayout>(R.id.ll_dialog_button_container) }
    private val mBtnDialogConfirm by lazy { mContentView.findViewById<Button>(R.id.btn_dialog_confirm) }
    private val mBtnDialogCancel by lazy { mContentView.findViewById<Button>(R.id.btn_dialog_cancel) }
    private val mTvDialogMessage by lazy { mContentView.findViewById<TextView>(R.id.tv_dialog_message) }
    private val mTvDialogTitle by lazy { mContentView.findViewById<TextView>(R.id.tv_dialog_title) }
    private val mIvDialogTop by lazy { mContentView.findViewById<ImageView>(R.id.iv_dialog_top) }

    private var mTopImageRes: Int = -1
    private var mTitleText: String = ""
    private var mMessageText: String = ""
    private var mCancelText: String = ""
    private var mConfirmText: String = ""
    private var mCallBack: ((Boolean) -> Unit)? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContentView =
            LayoutInflater.from(mContext).inflate(R.layout.layout_custom_tip_dialog, null)
        setContentView(mContentView)
        setListener()
        setCancelable(true)
        setCanceledOnTouchOutside(true)
    }

    private fun setListener() {
        mBtnDialogCancel.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                dismiss()
                mCallBack?.invoke(false)
            }
        }
        mBtnDialogConfirm.setOnClickListener {
            if (!Utils.isFastDoubleClick(it.id)) {
                dismiss()
                mCallBack?.invoke(true)
            }
        }
    }

    private fun setView() {
        if (mTopImageRes == -1) {
            mIvDialogTop.visibility = View.GONE
        } else {
            mIvDialogTop.visibility = View.VISIBLE
            mIvDialogTop.setImageResource(mTopImageRes)
        }
        if (TextUtils.isEmpty(mTitleText)) {
            mTvDialogTitle.visibility = View.GONE
        } else {
            mTvDialogTitle.visibility = View.VISIBLE
            mTvDialogTitle.text = mTitleText
        }
        if (TextUtils.isEmpty(mMessageText)) {
            mTvDialogMessage.visibility = View.GONE
        } else {
            mTvDialogTitle.visibility = View.VISIBLE
            mTvDialogMessage.text = mMessageText
        }
        if (TextUtils.isEmpty(mCancelText)) {
            mBtnDialogCancel.visibility = View.GONE
        } else {
            mBtnDialogCancel.visibility = View.VISIBLE
            mBtnDialogCancel.text = mCancelText
        }
        if (TextUtils.isEmpty(mConfirmText)) {
            mBtnDialogConfirm.visibility = View.GONE
        } else {
            mBtnDialogConfirm.visibility = View.VISIBLE
            mBtnDialogConfirm.text = mConfirmText
        }
        if (TextUtils.isEmpty(mCancelText) && TextUtils.isEmpty(mConfirmText)) {
            mllDialogButtonContainer.visibility = View.GONE
        } else {
            mllDialogButtonContainer.visibility = View.VISIBLE
        }
    }

    override fun onStart() {
        super.onStart()
        setView()
        window?.decorView?.setPadding(0, 0, 0, 0)
        window?.attributes?.gravity = Gravity.CENTER
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT
        )
    }

    fun setTopRes(res: Int): CustomTipDialog {
        mTopImageRes = res
        return this
    }

    fun setTitle(title: String): CustomTipDialog {
        mTitleText = title
        return this
    }

    fun setMessage(msg: String): CustomTipDialog {
        mMessageText = msg
        return this
    }

    fun setCancelText(cancelText: String): CustomTipDialog {
        mCancelText = cancelText
        return this
    }

    fun setConfirmText(confirmText: String): CustomTipDialog {
        mConfirmText = confirmText
        return this
    }

    fun setCallBack(callback: (Boolean) -> Unit): CustomTipDialog {
        mCallBack = callback
        return this
    }
}